package com.example.springboothello.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

public class FtpUtil {

    public static void main(String[] args) {
        downloadFile("C:\\Users\\JC\\nc\\1.jpg", "","/pub/himawari/L2/WLF/010/202310/07/10", "");
    }

    private static FTPClient ftpClient = new FTPClient();
    //ftp服务器ip地址
    private static final String FTP_ADDRESS = "ftp.ptree.jaxa.jp";
    //端口号
    private static final int FTP_PORT = 21;
    //用户名
    private static final String FTP_USERNAME = "2564167401_qq.com";
    //密码
    private static final String FTP_PASSWORD = "SP+wari8";


    /**
     * 从ftp下载文件
     * @param filename         需要下载的文件名
     * @param filePath         需要下载的文件路径
     * @param downloadPath     下载到本地的路径
     * @param downloadFilename 下载到本地的文件名
     */
    public static boolean downloadFile(String filename,String filePath, String downloadPath, String downloadFilename) {
        try {
            ftpClient.connect(FTP_ADDRESS, FTP_PORT);
            ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            if (!StringUtils.isEmpty(filename)) {
                filePath = filePath + "//" + filename;
            }

            ftpClient.enterLocalPassiveMode();
            FTPFile[] ftpFiles = ftpClient.listFiles(filePath);

            Arrays.sort(ftpFiles, new Comparator<FTPFile>() {
                @Override
                public int compare(FTPFile o1, FTPFile o2) {
                    return o1.getTimestamp().getTime().compareTo(o2.getTimestamp().getTime());
                }
            });

            // 下载指定文件
//            return ftpClient.retrieveFile(filename, out);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 断开连接
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
                ftpClient = null;
            }
        }
        return false;
    }

    /**
     * 上传文件到ftp
     *
     * @param file           文件
     * @param ftp_uploadPath 上传文件的路径
     * @param privateDir     私人目录
     * @param uploadFilename 上传的文件名称
     */
    public static boolean uploadFile(File file, String ftp_uploadPath, String privateDir, String uploadFilename) {

        try (InputStream in = new FileInputStream(file)) {

            // 连接FTP服务器并且验证用户名和密码
            ftpClient.connect(FTP_ADDRESS, FTP_PORT);
            ftpClient.login(FTP_USERNAME, FTP_PASSWORD);

            // 查看是否存在对应的目录
            boolean isChange = ftpClient.changeWorkingDirectory(ftp_uploadPath);

            if (!isChange) {
                // 创建个人目录
                ftpClient.makeDirectory(privateDir);
                // 切换
                ftpClient.changeWorkingDirectory(privateDir);
            }

            // 文件类型
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

            return ftpClient.storeFile(uploadFilename, in);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
